package com.imessage.action.manage.menu;

import com.bean.ManageResource;
import com.imessage.action.BaseAction;
import com.imessage.type.BaseState;
import com.imessage.type.ManageResourceType;
import com.imessage.vo.MenuVo;
import io.ebean.Expr;
import io.ebean.Expression;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


public class MenuBaseAction extends BaseAction {

    protected final String datagrid="datagrid-manageMenu-filter";


    protected void setExpressionList(List<Expression> expressionList, MenuVo menuVo){
        if(StringUtils.isNotBlank(menuVo.getName())){
            expressionList.add(Expr.contains("name", menuVo.getName()));
        }
        if(StringUtils.isNotBlank(menuVo.getUrl())){
            expressionList.add(Expr.contains("url", menuVo.getUrl()));
        }
    }


    protected void setModelAndView(ModelAndView modelAndView, ManageResource manageMenu){
        modelAndView.addObject("manageMenuForm", manageMenu);
        List<ManageResource> parentManageMenuList=getParentlyqMenuList();
        modelAndView.addObject("parentManageMenuList", parentManageMenuList);
        modelAndView.addObject("statusList", BaseState.values());
    }

    protected void setManageMenu(MenuVo menuVo, ManageResource manageMenu){
        copyProperties(menuVo, manageMenu);
        if(StringUtils.isNotBlank(menuVo.getState())){
            manageMenu.setState(BaseState.fromValue(menuVo.getState()));
        }
    }
    protected List<ManageResource> getParentlyqMenuList(){
        List<Expression> expressionList=new ArrayList<>();
        expressionList.add(Expr.eq("type",ManageResourceType.path.getCode()));
        List<ManageResource> parentlyqMenuList=new ArrayList<>();
        String order="seq asc";
        List<ManageResource> manageMenuList=
                baseService.findList(ManageResource.class, expressionList);
        if(manageMenuList==null
                ||manageMenuList.size()==0){
            return parentlyqMenuList;
        }
        for(Object o:manageMenuList){
            ManageResource manageMenu=(ManageResource)o;
            if(manageMenu.getManageHighResourceId()==null
                    ||manageMenu.getManageHighResourceId()==0){
                parentlyqMenuList.add(manageMenu);
            }
        }
        return parentlyqMenuList;
    }




}
